import { defineStore } from "pinia";
import router from "../../router";

export const useTagsViewStore = defineStore('tagsViewsStore',{
    state: () => ({
        activeTagsValue: '',
        visitedViews: [{
            path: '/home',
            name: 'Home',
            meta: { title: '首页', affix: true },
            title: '首页'
        }]
    }),
    getters: {},
    actions: {
        setTabsMenuValue(val: string) {
            this.activeTagsValue = val;
        },
        addVisitedView(view: any) {
            this.setTabsMenuValue(view.path);
            if (this.visitedViews.some(item => item.path === view.path)){
                return ;
            }
            this.visitedViews.push(Object.assign({},view,{
                title: view.meta.title || "no-name"
            }));
        },
        addView(view: any) {
            this.addVisitedView(view);
        },
        delVisitedView(path: string) {
             return new Promise(resolve => {
                 this.visitedViews = this.visitedViews.filter(item => {
                     return item.path != path || item.meta.affix
                 });
                 resolve([...this.visitedViews])
             });
        },
        delView(activeTabPath: string) {
            return new Promise(resolve => {
                this.delVisitedView(activeTabPath);
                resolve({
                    visitedViews: [...this.visitedViews]
                });
            });
        },
        delOtherViews(path: string) {
            this.visitedViews = this.visitedViews.filter(item => {
                return item.path === path || item.meta.affix;
            });
        },
        delAllViews() {
           return new Promise(resolve => {
               this.visitedViews = this.visitedViews.filter(item => item.meta.affix);
               resolve([...this.visitedViews]);
           });
        },
        goHome(){
            this.activeTagsValue = '/home';
            router.push({path: '/home'}).then(r => {});
        }
    }
})